/**
 * WindowSaver.java 2008-05-16
 * 
 * Linker is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

package linker.gui;

import linker.account.ManageAccount;

import java.awt.AWTEvent;
import java.awt.Dimension;
import java.awt.event.AWTEventListener;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.swing.JFrame;

/**
 * Save the windows' location and size. This class is to save the changes of
 * current window, which makes it possible for you to see the same size on the
 * same computer the next time you start the application.
 * 
 * But now it has been abused. We need to control some of the uses
 * 
 * @version 2008-05-16
 * @author Jianfeng tujf.cn@gmail.com
 * @author AwakenRz awakenrz@gmail.com
 * 
 */

public class WindowSaver implements AWTEventListener {

	/**
	 * Singleton self refer.
	 */
	private static WindowSaver windowSaver;

	/**
	 * The full path of window config.
	 */
	private static String path = ManageAccount.getConfigPath() + "windowConfig";

	/**
	 * Get the single WindowSaver.
	 * 
	 * @return The single WindowSaver.
	 */
	public static WindowSaver getWindowSaver() {
		if (windowSaver == null) {
			windowSaver = new WindowSaver();
		}

		return windowSaver;
	}

	/**
	 * @param e
	 *            AWTEvent.
	 */
	public final void eventDispatched(final AWTEvent e) {
		try {
			if (e.getID() == WindowEvent.WINDOW_DEACTIVATED) {
				ComponentEvent cev = (ComponentEvent) e;
				if (cev.getComponent() instanceof JFrame) {
					JFrame frame = (JFrame) cev.getComponent();
					saveSettings(frame);
				}
			}

		} catch (Exception ee) {
			ee.printStackTrace();
		}
	}

	/**
	 * Load saved Window config.
	 * 
	 * @param frame
	 *            The frame of which ,its config is saved.
	 * @throws IOException
	 *             The Exception of file error.
	 */
	public static void loadSettings(final JFrame frame) throws IOException {
		Properties settings = new Properties();

		try {
			settings.load(new FileInputStream(path));
		} catch (FileNotFoundException fnfe) {
			settings.store(new FileOutputStream(path), "Window settings");
		}

		String name = frame.getName();
		int x = getInt(settings, name + ".x", -1);
		int y = getInt(settings, name + ".y", -1);
		int w = getInt(settings, name + ".w", -1);
		int h = getInt(settings, name + ".h", -1);

		if (x != -1 && y != -1) {
			frame.setLocation(x, y);
		}
		if (w != -1 && h != -1) {
			frame.setSize(new Dimension(w, h));
		}

		frame.validate();
	}

	/**
	 * Get in value from settings by the name of window.
	 * 
	 * @param settings
	 *            The whole data of window config.
	 * @param name
	 *            The name of value.
	 * @param i
	 *            The default value.
	 * @return The int value.
	 */
	private static int getInt(final Properties settings, final String name,
			final int i) {
		String v = settings.getProperty(name);
		if (v == null) {
			return i;
		}

		return Integer.parseInt(v);
	}

	/**
	 * Save window settings by window.
	 * 
	 * @param frame
	 *            The saved window.
	 * @throws IOException
	 *             The Exception of file write error.
	 */
	public static void saveSettings(final JFrame frame) throws IOException {
		Properties settings = new Properties();
		String name = frame.getName();

		try {
			settings.load(new FileInputStream(path));
		} catch (FileNotFoundException fnfe) {
			fnfe.printStackTrace();
		}

		settings.put(name + ".x", "" + frame.getX());
		settings.put(name + ".y", "" + frame.getY());
		settings.put(name + ".w", "" + frame.getWidth());
		settings.put(name + ".h", "" + frame.getHeight());

		settings.store(new FileOutputStream(path), null);
	}
}
